﻿using System;
namespace ORS.OrderRobot
{
    /// <summary>
    /// 周期任务基类
    /// </summary>
    public abstract class PeriodicBackgroundWorkerBase : IBackgroundWorker
    {
        public abstract string Name { get; }
        protected abstract TimeSpan Period { get; }

        private Timer? _timer;

        public virtual Task StartAsync(CancellationToken cancellationToken = default)
        {
            _timer = new Timer(async _ => await DoWorkAsync(), null, TimeSpan.Zero, Period);
            return Task.CompletedTask;
        }

        public virtual Task StopAsync(CancellationToken cancellationToken = default)
        {
            _timer?.Dispose();
            return Task.CompletedTask;
        }
        /// <summary>
        /// 具体的任务实现类
        /// </summary>
        /// <returns></returns>
        protected abstract Task DoWorkAsync();
    }
}

